
if SERVER then
	AddCSLuaFile("shared.lua")
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
end

if ( CLIENT ) then
	SWEP.ViewModelFOV		= 75
	SWEP.ViewModelFlip		= false
	SWEP.CSMuzzleFlashes	= false
	SWEP.Slot				= 1
	SWEP.SlotPos			= 1

	SWEP.PrintName			= "Knife"			
	SWEP.IconLetter			= "j"
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false

	killicon.AddFont( "weapon_knife_n", "CSKillIcons", "j", Color( 255, 80, 0, 255 ) )
    
end

SWEP.Base				= "tp_base"

SWEP.Weight = 5;
SWEP.AutoSwitchTo = true;
SWEP.AutoSwitchFrom = false; 

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.Primary.Recoil			= 4.6
SWEP.Primary.Damage			= 10
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.02
SWEP.Primary.Delay			= .7

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.ViewModel = "models/weapons/v_knife_t.mdl"
SWEP.WorldModel = "models/weapons/w_knife_t.mdl" 

SWEP.Swing = Sound("weapons/knife/knife_slash1.wav")
SWEP.HitSound = Sound("weapons/knife/knife_hitwall1.wav")
SWEP.FleshHit = {"weapons/knife/knife_hit1.wav",
"weapons/knife/knife_hit2.wav",
"weapons/knife/knife_hit3.wav",
"weapons/knife/knife_hit4.wav",
"weapons/knife/knife_stab.wav"}

function SWEP:Initialize()
    if (SERVER) then
		self:SetWeaponHoldType("melee")
	end
	for k,v in pairs(self.FleshHit) do
		util.PrecacheSound(v)
	end
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
end 

function SWEP:Think()

end

function SWEP:CanPrimaryAttack()
	return true
end

function SWEP:DrawHUD()
	local w,h = ScrW(),ScrH()
	
	surface.SetDrawColor(220, 0, 0, 220)
	local wh, lh, sh = w*.5, h*.5, 10
	surface.DrawLine(wh - sh, lh - sh, wh + sh, lh - sh) //top line
	surface.DrawLine(wh - sh, lh + sh, wh + sh, lh + sh) //bottom line
	surface.DrawLine(wh - sh, lh - sh, wh - sh, lh + sh) //left line
	surface.DrawLine(wh + sh, lh - sh, wh + sh, lh + sh) //right line
end

function SWEP:PrimaryAttack()
	
	if not self:CanPrimaryAttack() then return end
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	
	self.Weapon:EmitSound(self.Swing,100,math.random(90,120))
	self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
	
 	local trace = util.GetPlayerTrace(self.Owner)
 	local tr = util.TraceLine(trace)

	if (self.Owner:GetPos() - tr.HitPos):Length() > 100 then
		return
	end

	if tr.HitWorld then
		util.Decal("ManhackCut", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
		self.Weapon:EmitSound(self.HitSound,100,math.random(95,110))
		self.Owner:ViewPunch(Angle( math.Rand(-3,3) * self.Primary.Recoil, math.Rand(-3,3) * self.Primary.Recoil, 0 ) )
	elseif tr.HitNonWorld and tr.MatType == MAT_GLASS then
		self.Weapon:EmitSound(self.HitSound,100,math.random(90,110))
		return
	elseif tr.HitNonWorld and tr.Entity:GetClass() == "func_breakable" then
		self.Weapon:EmitSound(self.HitSound,100,math.random(90,110))
		if SERVER then
			tr.Entity:TakeDamage(self.Primary.Damage + math.random(5,10),self.Owner) 
		end
		return
	elseif tr.Entity:IsPlayer() or string.find(tr.Entity:GetClass(),"npc")  then
		self.Owner:ViewPunch(Angle( math.Rand(-3,3) * self.Primary.Recoil, math.Rand(-3,3) * self.Primary.Recoil, 0 ) )
		util.Decal("Blood", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
		self.Weapon:EmitSound(ChooseRandom(self.FleshHit),100,math.random(90,110))
		if SERVER then
			tr.Entity:VoiceSound(ChooseRandom(tr.Entity.Pain),100)
			local damage = math.random(1,15)
			tr.Entity:TakeDamage(self.Primary.Damage + damage,self.Owner) 
			tr.Entity:AddAttackerDamage(self.Owner,self.Primary.Damage + damage,false)
			local effectdata = EffectData()
			effectdata:SetOrigin(tr.HitPos)
			effectdata:SetNormal(tr.HitNormal)
			util.Effect("bodyshot", effectdata)
			util.Decal("Blood", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
			if string.find(tr.Entity:GetClass(),"npc") then
				local vel = self.Owner:EyeAngles():Forward() * 300
				vel.z = 200
				tr.Entity:SetVelocity(vel)
			end
		end
	elseif tr.HitNonWorld and not tr.Entity:IsPlayer() then  
		if SERVER then
			util.Decal("ManhackCut", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
			tr.Entity:TakeDamage(math.random(10,15),self.Owner) 
			local phys = tr.Entity:GetPhysicsObject()
			if phys then
				if phys:IsValid() then
					phys:ApplyForceCenter(self.Owner:GetAimVector() * 1200)
				end
			end
		end
		self.Weapon:EmitSound(self.HitSound,100,math.random(95,110))
	end
end
 
function SWEP:SecondaryAttack()

end

